package com.nit.bike.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.bike.dto.utils.OpResultDTO;
import com.nit.bike.service.basic.StatisticsService;
import com.nit.bike.utils.AuthorizationUtil;
import com.nit.bike.utils.HttpRequestReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/stats")
public class StatisticsController {
    @Resource
    private StatisticsService statisticsService;
    private final Logger logger = LoggerFactory.getLogger(StatisticsController.class);

    /**
     * 统计用户总数和增长数
     */
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public OpResultDTO statsUser(@RequestHeader("Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject json = HttpRequestReader.getJsonObject(request);
                op = statisticsService.statsUser(json);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return op;
    }

    /**
     * 统计车辆信息
     */
    @RequestMapping(value = "/bikeTotal", method = RequestMethod.POST)
    public OpResultDTO bikeTotal(@RequestHeader("Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject json = HttpRequestReader.getJsonObject(request);
                op = statisticsService.bikeTotal(json);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return op;
    }

    /**
     * 充值和消费总计
     */
    @RequestMapping(value = "/payTotal", method = RequestMethod.POST)
    public OpResultDTO payTotal(@RequestHeader("Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject json = HttpRequestReader.getJsonObject(request);
                op = statisticsService.payTotal(json);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return op;
    }

    /**
     * 骑行总时间和平均时间
     */
    @RequestMapping(value = "/duringTotal", method = RequestMethod.POST)
    public OpResultDTO duringTotal(@RequestHeader("Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject json = HttpRequestReader.getJsonObject(request);
                op = statisticsService.duringTotal(json);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return op;
    }

    /**
     * 统计区间骑行时长和消费
     */
    @RequestMapping(value = "/duringAndCost", method = RequestMethod.POST)
    public OpResultDTO duringAndCost(@RequestHeader("Access-Token") String token, HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            if (AuthorizationUtil.adminInterfaceBool(token, op)) {
                JSONObject json = HttpRequestReader.getJsonObject(request);
                op = statisticsService.duringAndCost(json);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return op;
    }
}
